package com.junlong0716.enjoylearning.ui

import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.constant.NetConstant.REQUEST_SOURCE
import com.junlong0716.enjoylearning.entity.*
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.module.common.base.BasePresenter
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.trello.rxlifecycle2.components.support.RxFragment
import org.json.JSONObject

/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 上午11:53 2018/4/21
 *@modified by:
 */
class FindPresenter : BasePresenter<FindContract.View>(), FindContract.Presenter {
    private lateinit var commendVideoData: ArrayList<FindItemEntity>
    private lateinit var commendVideoItemData: ArrayList<FindCommendVideoEntity>

    private lateinit var commendAudioData: ArrayList<FindItemEntity>
    private lateinit var commendAudioItemData: ArrayList<FindCommendAudioEntity>

    private lateinit var commendDocumentData: ArrayList<FindItemEntity>
    private lateinit var commendDocumentItemData: ArrayList<FindCommendDocumentEntity>

    private lateinit var bannerData: ArrayList<FindBannerEntity>
    private lateinit var headerData: ArrayList<FindItemEntity>

    private lateinit var commendTopicData: ArrayList<FindItemEntity>
    private lateinit var commendTopicItemData: ArrayList<FindCommendTopicEntity>


    override fun requestCommandKnowledge(isRandom: Boolean, fragment: RxFragment) {
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_COMMAND_KNOWLEDGE)
                .tag(fragment)
                .cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)
                .cacheKey("CATCH_COMMAND_KNOWLEDGE")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        initData(response)
                    }

                    override fun onCacheSuccess(response: Response<String>?) {
                        initData(response)
                    }

                    override fun onError(response: Response<String>?) {
                        super.onError(response)
                    }
                })
    }

    private fun initData(response: Response<String>?) {
        val jsonObj = JSONObject(response!!.body())
        val code = jsonObj.getInt("code")
        if (code == 200) {
            commendVideoData = ArrayList()
            commendVideoItemData = ArrayList()

            commendAudioData = ArrayList()
            commendAudioItemData = ArrayList()

            commendDocumentData = ArrayList()
            commendDocumentItemData = ArrayList()

            commendTopicData = ArrayList()
            commendTopicItemData = ArrayList()

            val dataMap = jsonObj.getJSONObject("dataMap")
            val special = dataMap.getJSONArray("special")
            val video = dataMap.getJSONArray("video")
            val audio = dataMap.getJSONArray("audio")
            val article = dataMap.getJSONArray("article")

            for (i in 0 until video.length()) {
                commendVideoItemData.add(FindCommendVideoEntity(video.getJSONObject(i).getInt("id"), video.getJSONObject(i).getString("coverImg"), video.getJSONObject(i).getString("knowledgeTitle"), video.getJSONObject(i).getString("fileAddress"), video.getJSONObject(i).getString("authorName"), video.getJSONObject(i).getLong("createDate")))
            }
            for (i in 0 until audio.length()) {
                commendAudioItemData.add(FindCommendAudioEntity(audio.getJSONObject(i).getInt("id"), audio.getJSONObject(i).getString("coverImg"), audio.getJSONObject(i).getString("knowledgeTitle"), audio.getJSONObject(i).getString("fileAddress"), audio.getJSONObject(i).getString("authorName"), audio.getJSONObject(i).getLong("createDate")))
            }
            for (i in 0 until article.length()) {
                commendDocumentItemData.add(FindCommendDocumentEntity(article.getJSONObject(i).getInt("id"), article.getJSONObject(i).getString("knowledgeTitle"), article.getJSONObject(i).getString("knowledgeDescribe"), article.getJSONObject(i).getString("knowledgeContent"), article.getJSONObject(i).getString("fileAddress"), article.getJSONObject(i).getString("authorName"), article.getJSONObject(i).getLong("createDate")))
            }
            for (i in 0 until special.length()) {
                val praiseNum = try {
                    special.getJSONObject(i).getInt("praiseNumber")
                } catch (e: Exception) {
                    0
                }
                val isPraise = try {
                    special.getJSONObject(i).getInt("isPraise")

                } catch (e: Exception) {
                    0
                }
                commendTopicItemData.add(FindCommendTopicEntity(special.getJSONObject(i).getInt("id"), special.getJSONObject(i).getString("coverImg"), special.getJSONObject(i).getString("specialTitle"), special.getJSONObject(i).getString("knowledgeDescribe"), special.getJSONObject(i).getLong("createDate"), isPraise,praiseNum))
            }

//            commendTopicItemData.add(FindCommendTopicEntity("http://img2.imgtn.bdimg.com/it/u=1535191899,3259187997&fm=27&gp=0.jpg", "广告法", "《中华人民共和国广告法》已由中华人民共和国第八届全国人民代表大会常务委员会第十次会议于1994年10月27日通过，现予公布，自1995年2月1日起施行。", 1, "违背“社会良好风尚”是什么标准？"))
//            commendTopicItemData.add(FindCommendTopicEntity("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1526226404552&di=c437740aec82522b07ea3ff27f14ab8b&imgtype=0&src=http%3A%2F%2Fs7.sinaimg.cn%2Fmw690%2F0042M7VMzy765WrrwTsc6", "庖丁解牛（周二）-典型案例分析", "解析各种各样的经典案例。", 1, "查处违背社会良好风尚违法广告，保护妇女、未成年人合法权益及身心健康- 2018/1/2 11.00"))
//            commendTopicItemData.add(FindCommendTopicEntity("http://pic28.photophoto.cn/20130907/0007019994629037_b.jpg", "商品科-张雪", "隶属商品科相关专题", 1, "商品科工作特色--小雪带你读“标准”第2期生活用纸选购"))
            commendTopicData.add(FindItemEntity(ArrayList(), commendTopicItemData, ArrayList(), ArrayList(), ArrayList()))
            commendVideoData.add(FindItemEntity(ArrayList(), ArrayList(), commendVideoItemData, ArrayList(), ArrayList()))
            commendAudioData.add(FindItemEntity(ArrayList(), ArrayList(), ArrayList(), commendAudioItemData, ArrayList()))
            commendDocumentData.add(FindItemEntity(ArrayList(), ArrayList(), ArrayList(), ArrayList(), commendDocumentItemData))
            getView()!!.requestCommandKnowledgeSucceed(commendVideoData, commendAudioData, commendDocumentData, commendTopicData)
        }
    }


    override fun requestBanner(fragment: RxFragment) {
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_KNOWLEDGE_BANNER)
                .tag(fragment)
                .cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)
                .cacheKey("CATCH_COMMAND_KNOWLEDGE_BANNER")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        initBanner(response)
                    }

                    override fun onCacheSuccess(response: Response<String>?) {
                        initBanner(response)
                    }

                })
    }

    private fun initBanner(response: Response<String>?) {
        val jsonObject = JSONObject(response!!.body())
        val code = jsonObject.getInt("code")
        if (code == 200) {
            headerData = ArrayList()
            bannerData = ArrayList()

            val data = jsonObject.getJSONArray("data")
            for (i in 0 until data.length()) {
                bannerData.add(FindBannerEntity(REQUEST_SOURCE + data.getJSONObject(i).getString("fileAddress"), data.getJSONObject(i).getInt("specialId"), data.getJSONObject(i).getInt("type"), data.getJSONObject(i).getString("urlAddress")))
            }
            headerData.add(FindItemEntity(bannerData, ArrayList(), ArrayList(), ArrayList(), ArrayList()))
        }

        getView()!!.requestBannerSucceed(headerData)
    }


    override fun onDestroy() {
    }
}